package util

import (
	//"fmt"
	"strconv"
	"html/template"
    //"utils/web"
    "io/ioutil"
	//"strings"
	//"time"
	"bytes"

    "utils/mgo/bson"

    //"fmt"
    "time"
    "encoding/binary"
	"encoding/hex"
    //"regexp"

)
//定义用户信息
type Thtml struct {
    //ctx *web.Context             
    tpl   *template.Template       //
    Dir   string                  //模板目录
    Data  map[interface{}]interface{}
    Mark  int
}

//页面计数器
var TplMark int = 0
var TplMark2 int = 0

var TplGroup map[int]string

/**
 * 读取整个目录进入缓存
 *
 * author: indraw
 * date: 2013-01-01
 */
func  NewThtml(dir string ) *Thtml {
    
    return &Thtml{Dir:dir,
                  Data:make(map[interface{}]interface{}),
    }
}

/**
 * 读取整个目录进入缓存
 *
 * author: indraw
 * date: 2013-01-01
 */
func  (t *Thtml) Assign( name interface{}, value interface{} ) {
    
    t.Data[name] = value;
}
 
/**
 * 输出一个页面
 *
 * author: indraw
 * date: 2013-01-01
 */
func (t *Thtml) Display(file string) string {
    t.tpl = template.New(file)

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplObjectID": GetTplObjectID} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplObjectTime": GetTplObjectTime} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplTopID": GetTplTopID} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplDownID": GetTplDownID} )

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplGroupName": GetTplGroupName} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplTime": GetTplTime} )

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplFormatMax": TplFormatMax} )

    t.tpl = t.tpl.Funcs( template.FuncMap{"eq": TplSignEq} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"neq": TplSignNeq} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"gre": TplSignGre} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"les": TplSignLes} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"have": TplCheckHave} )

    t2, _ := t.tpl.ParseFiles(t.Dir + "header.html", t.Dir + file, t.Dir + "footer.html")

    newbytes := bytes.NewBufferString("")

    t2.ExecuteTemplate(newbytes, "header", t.Data)
    t2.ExecuteTemplate(newbytes, "content", t.Data)
    t2.ExecuteTemplate(newbytes, "footer", t.Data)


    tplcontent, _ := ioutil.ReadAll(newbytes)
    
    TplMark = 0
    TplMark2 = 1
    
    return string(tplcontent)
}
 
/**
 * 输出成功提示
 *
 * author: indraw
 * date: 2013-01-01
 */
func (t *Thtml) Success(data string) string {
		
    t.tpl = template.New("success.html")

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplObjectID": GetTplObjectID} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplObjectTime": GetTplObjectTime} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplTopID": GetTplTopID} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplDownID": GetTplDownID} )

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplGroupName": GetTplGroupName} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"eq": TplSignEq} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"neq": TplSignNeq} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"gre": TplSignGre} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"les": TplSignLes} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"have": TplCheckHave} )
    
    t2, _ := t.tpl.ParseFiles(t.Dir + "header.html", t.Dir + "success.html", t.Dir + "footer.html")

    newbytes := bytes.NewBufferString("")

    t.Data["Message"] = data

    t2.ExecuteTemplate(newbytes, "header", t.Data)
    t2.ExecuteTemplate(newbytes, "content", t.Data)
    t2.ExecuteTemplate(newbytes, "footer", t.Data)
    
    tplcontent, _ := ioutil.ReadAll(newbytes)
    
    return string(tplcontent)
}

/**
 * 输出错误提示
 *
 * author: indraw
 * date: 2013-01-01
 */
func (t *Thtml) Error( data string) string {
    t.tpl = template.New("error.html")

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplObjectID": GetTplObjectID} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplObjectTime": GetTplObjectTime} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplTopID": GetTplTopID} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"TplDownID": GetTplDownID} )

    t.tpl = t.tpl.Funcs( template.FuncMap{"TplGroupName": GetTplGroupName} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"eq": TplSignEq} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"neq": TplSignNeq} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"gre": TplSignGre} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"les": TplSignLes} )
    t.tpl = t.tpl.Funcs( template.FuncMap{"have": TplCheckHave} )
    
    t2, _ := t.tpl.ParseFiles(t.Dir + "header.html", t.Dir + "error.html", t.Dir + "footer.html")
    newbytes := bytes.NewBufferString("")

    t.Data["Message"] = data
    t2.ExecuteTemplate(newbytes, "header", t.Data)
    t2.ExecuteTemplate(newbytes, "content", t.Data)
    t2.ExecuteTemplate(newbytes, "footer", t.Data)
    
    tplcontent, _ := ioutil.ReadAll(newbytes)
    
    return string(tplcontent)
}


/**
 * 获取ID
 *
 * author: indraw
 * date: 2013-01-01
 */
func GetTplObjectID(id bson.ObjectId) string {
            
    return hex.EncodeToString([]byte(id))

}

/**
 * 显示时间
 *
 * author: indraw
 * date: 2013-01-01
 */
func GetTplObjectTime(id bson.ObjectId, format string) string {
    
	secs := int64(binary.BigEndian.Uint32([]byte(string(id)[0:4])))
    
    if format == "" {
        format = "2006-01-02 15:04"
    }
        
	return time.Unix(secs, 0).Format(format)
    
}

/**
 * 显示页面时间
 *
 * author: indraw
 * date: 2013-01-01
 */

func GetTplTime(sec interface{}, format string) string {
    
    var sec64 int64
    
    switch v := sec.(type){
        case int:
            sec64 = int64(v)
        case string:
            i, err := strconv.Atoi(v)
            if err != nil {
                return ""
            }
            sec64 = int64(i)
        default:
            return ""
    }
    
    tt := time.Unix( int64( sec64 ), 0 )
    
    if format == "" {
        format = "2006-01-02 15:04"
    }
    
    return tt.Format(format)
   
}

/**
 * 显示页面ID
 *
 * author: indraw
 * date: 2013-01-01
 */

func GetTplTopID(id int) int {
    
    id = id - TplMark
    TplMark++
    
    return id
}

/**
 * 显示页面ID
 *
 * author: indraw
 * date: 2013-01-01
 */

func GetTplDownID(id int) int {
    
    id = id + TplMark2
    TplMark2++
    
    return id
}

/**
 * 显示页面分组名称
 *
 * author: indraw
 * date: 2013-01-01
 */

func GetTplGroupName(id int) string {
        
    return TplGroup[id]
}

/**
 * 等于
 *
 * author: indraw
 * date: 2013-01-01
 */

func TplSignEq(s1  interface{} ,s2  interface{}) bool {
    if s1 == s2{
        return true
    }
    return false
}

/**
 * 等于
 *
 * author: indraw
 * date: 2013-01-01
 */
/*
func TplSignEq(s1 string ,s2 string) bool {
	i1, _ := strconv.Atoi(s1)
	i2, _ := strconv.Atoi(s2)

    if i1 == i2{
        return true
    }
    return false
}
*/
/**
 * 不等于
 *
 * author: indraw
 * date: 2013-01-01
 */

func TplSignNeq(s1  interface{} ,s2  interface{}) bool {
    if s1 != s2{
        return true
    }
    return false
}

/**
 * 大于
 *
 * author: indraw
 * date: 2013-01-01
 */

func TplSignGre(s1 int ,s2 int) bool {
    if s1 > s2{
        return true
    }
    return false
}

/**
 * 不等于
 *
 * author: indraw
 * date: 2013-01-01
 */

func TplSignLes(s1 int ,s2 int) bool {
    /*
    i1,_ := strconv.Atoi(s1)

    i2,_ := strconv.Atoi(s2)
    * */
    if s1 < s2{
        return true
    }
    return false
}

/**
 * 不等于
 *
 * author: indraw
 * date: 2013-01-01
 */

func TplCheckHave(s1 string) bool {

    if len(s1) > 0 {
        return true
    }
    return false
}

/**
 * 输入数据
 *
 * author: indraw
 * date: 2013-01-01
 */

/**
 * 输入数据长度格式化
 *
 * author: indraw
 * date: 2013-01-01
 */

func TplFormatMax(s1 string,len int) string {

    
    return s1
}



